package com.tangula.web.kashine.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.tangula.controller.ReturnRespController;
import com.tangula.core.Context;
import com.tangula.core.Controller;
import com.tangula.web.config.auth.Ignore;
import com.tangula.web.form.DefaultPagingBody;
import com.tangula.web.form.IdForm;
import com.tangula.web.kashine.business.OrgBusiness;
import com.tangula.web.kashine.business.ProductBusiness;
import com.tangula.web.kashine.business.StoreBusiness;
import com.tangula.web.kashine.business.StudentBusiness;
import com.tangula.web.kashine.business.TeacherBusiness;
import com.tangula.web.kashine.entity.AccountEntity;
import com.tangula.web.kashine.entity.BaseRespEntity;
import com.tangula.web.kashine.entity.EmailForm;
import com.tangula.web.kashine.entity.FreeTeacherModule;
import com.tangula.web.kashine.entity.IdNameDescIconForm;
import com.tangula.web.kashine.entity.IntegerForm;
import com.tangula.web.kashine.entity.OrgBlockForm;
import com.tangula.web.kashine.entity.OrgForm;
import com.tangula.web.kashine.entity.PagableForm;
import com.tangula.web.kashine.entity.ProductEntity;
import com.tangula.web.kashine.entity.ProductForm;
import com.tangula.web.kashine.entity.StoreEntity;
import com.tangula.web.kashine.entity.StoreForm;
import com.tangula.web.kashine.entity.StorePageableForm;
import com.tangula.web.kashine.entity.StudentBlockForm;
import com.tangula.web.kashine.entity.StudentForm;
import com.tangula.web.kashine.entity.TeacherBlockForm;
import com.tangula.web.kashine.entity.TeacherForm;

@RestController
@RequestMapping("/adminController")
@ResponseBody
public class AdminController implements Controller<Context>, ReturnRespController, AuthController {
	@Autowired
	OrgBusiness orgBusiness;
	@Autowired
	TeacherBusiness bizTeacher;
	@Autowired
	StudentBusiness bizStudent;
	@Autowired
	StoreBusiness bizStore;
	@Autowired
	ProductBusiness productBusiness;

	/**
	 * 管理员查看组织
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/loadOrgs", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<DefaultPagingBody<AccountEntity>>> loadOrg(@RequestBody PagableForm pag) {
		return returnSuccess(orgBusiness.loadOrg(pag));
	}

	/**
	 * 管理员查看教师
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/loadTeacher", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<DefaultPagingBody<AccountEntity>>> loadTeacher(@RequestBody PagableForm pag) {
		return returnSuccess(bizTeacher.loadTeachers(pag));
	}

	/**
	 * 管理员查看学生
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/loadAllStudents", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<DefaultPagingBody<AccountEntity>>> loadAllStudents(@RequestBody PagableForm page) {
		return returnSuccess(bizStudent.loadAllStudents(page));
	}

	/**
	 * 管理员查看商店
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/loadStores", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<DefaultPagingBody<StoreEntity>>> loadStores(@RequestBody PagableForm page) {
		logInfo("invoke admin controller loadStores");
		return returnSuccess(bizStore.loadStore(page));
	}

	/**
	 * 管理员查看所选组织的成员个数
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/loadOrgSize", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<IntegerForm>> loadOrgSize(@RequestBody OrgForm org) {
		return returnSuccess(orgBusiness.loadOrgSize(org));
	}

	/**
	 * 管理员查看组织是否被锁
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/putBlockOrUnBlockOrg", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<AccountEntity>> putBlockOrUnBlockOrg(@RequestBody OrgForm org) {
		return returnSuccess(orgBusiness.blockOrUnBlockOrg(org));
	}

	/**
	 * 管理员封锁商店
	 *
	 * @param form .
	 * @return .
	 */
	@RequestMapping(value = "/blockStore", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> blockStore(@RequestBody IdForm form) {
		if (StringUtils.equalsIgnoreCase(loadCurrentAccount().getType(), "admin")) {
			return returnSuccess(bizStore.block(form.getId()));
		}
		return returnSuccess(false);
	}

	/**
	 * 管理员封锁商店
	 *
	 * @param form .
	 * @return .
	 */
	@RequestMapping(value = "/unblockStore", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> unblockStore(@RequestBody IdForm form) {
		if (StringUtils.equalsIgnoreCase(loadCurrentAccount().getType(), "admin")) {
			return returnSuccess(bizStore.unblock(form.getId()));
		}
		return returnSuccess(false);
	}

	/**
	 * 管理员封锁组织
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/putBlockOrg", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> putBlockOrg(@RequestBody IdForm form) {
		return returnSuccess(true);
	}

	/**
	 * 管理员解除组织封锁
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/putUnblockOrg", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> putUnblockOrg(@RequestBody OrgBlockForm org) {
		return returnSuccess(orgBusiness.unblockOrg(org) != null);
	}

	/**
	 * 管理员查看选定教师
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/loadSeleteTeacher", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<AccountEntity>> loadSeleteTeacher(@RequestBody TeacherForm teacher) {
		return returnSuccess(bizTeacher.seleteTeacher(teacher));
	}

	/**
	 * 管理员查看教师是否被锁
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/putBlockOrUnblockTeacher", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<AccountEntity>> putBlockOrUnblockTeacher(@RequestBody TeacherForm teacher) {
		return returnSuccess(bizTeacher.blockOrUnblockTeacher(teacher));
	}

	/**
	 * 管理员封锁教师
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/putBlockTeacher", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> putBlockTeacher(@RequestBody TeacherBlockForm teacher) {
		return returnSuccess(bizTeacher.blockTeacher(teacher) != null);
	}

	/**
	 * 管理员解除教师封锁
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/putUnblockTeacher", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> putUnblockTeacher(@RequestBody TeacherBlockForm teacher) {
		return returnSuccess(bizTeacher.unblockTeacher(teacher) != null);
	}

	/**
	 * 管理员查看选定学生
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/loadSeleteStudent", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<AccountEntity>> loadSeleteStudent(@RequestBody StudentForm student) {
		return returnSuccess(bizStudent.seleteStudent(student));
	}

	/**
	 * 管理员查看学生是否被锁
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/putBlockOrUnblockStudent", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<AccountEntity>> putBlockOrUnblockStudent(@RequestBody StudentForm student) {
		return returnSuccess(bizStudent.blockOrUnblockStudent(student));
	}

	/**
	 * 管理员封锁学生
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/putBlockStudent", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> putBlockStudent(@RequestBody StudentBlockForm student) {
		return returnSuccess(bizStudent.blockStudent(student) != null);
	}

	/**
	 * 管理员解除学生封锁
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/putUnblockStudent", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> putUnblockStudent(@RequestBody StudentBlockForm student) {
		return returnSuccess(bizStudent.unblockStudent(student) != null);
	}

	/**
	 * 管理员查看选定商店
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/loadSelectStore", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<StoreEntity>> loadSelectStore(@RequestBody StoreForm store) {
		return returnSuccess(bizStore.seleteStore(store));
	}

	/**
	 * 管理员修改选定商店
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/putChangeStore", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> putChangeStore(@RequestBody StoreEntity store) {
		return returnSuccess(bizStore.changeStore(store) != null);
	}

	/**
	 * 管理员删除选定商店
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/putDeletStore", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> putDeletStore(@RequestBody StoreForm store) {
		return returnSuccess(bizStore.deletStore(store));
	}

	/**
	 * 管理员查看选定商店中的商品
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/loadProductInStore", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<DefaultPagingBody<ProductEntity>>> loadProduct(
			@RequestBody StorePageableForm storePag) {
		return returnSuccess(productBusiness.loadProductInStore(storePag));
	}

	/**
	 * 管理员修改选定的商品
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/putChangeProduct", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> putChangeProduct(@RequestBody ProductEntity product) {
		return returnSuccess(productBusiness.changeProduct(product) != null);
	}

	/**
	 * 管理员删除选定的商品
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/putDeleteProduct", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> putDeleteProduct(@RequestBody ProductForm product) {
		return returnSuccess(productBusiness.deleteProduct(product));
	}

	/**
	 * 修改密码
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/updateAccountPin", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> updateAccountPin(@RequestBody EmailForm emailForm) {
		return returnSuccess(bizStudent.changePassword(emailForm) != null);
	}

	/**
	 * 加载没有加入组织的教师
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/loadFreeTeachers", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<DefaultPagingBody<FreeTeacherModule>>> loadFreeTeachers(
			@RequestBody PagableForm pag) {
		return returnSuccess(bizTeacher.loadFreeTeacher(pag));
	}

	/**
	 * 查看Email是否存在
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/validateExistsAccount", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> validateExistsAccount(@RequestBody EmailForm emailForm) {
		return returnSuccess(bizStudent.validateExistsAccount(emailForm) != null);
	}

	/**
	 * 查看Email是否存在
	 *
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/updateStore", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> updateStore(@RequestBody IdNameDescIconForm form) {
		if (!StringUtils.equalsIgnoreCase(loadCurrentAccount().getType(), "admin")) {
			return returnSuccess(false);	
		}
		StoreEntity entity = new StoreEntity();
		entity.setId(form.getId());
		entity.setStoreName(form.getName());
		entity.setStoreDescription(form.getDesc());
		entity.setStoreImage(form.getIcon());
		return returnSuccess(bizStore.updateNameDescIcon(entity));
	}
	
	@RequestMapping(value = "/removeStore", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> removeStore(@RequestBody IdForm form) {
		if (!StringUtils.equalsIgnoreCase(loadCurrentAccount().getType(), "admin")) {
			return returnSuccess(false);	
		}
		return returnSuccess(bizStore.removeStore(form.getId()));
	}
	
	


	@RequestMapping(value = "/blockStudent", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> blockStudent(@RequestBody IdForm form) {
		if (StringUtils.equalsIgnoreCase(loadCurrentAccount().getType(), "admin")) {
			return returnSuccess(bizStudent.block(form.getId()));
		}
		return returnSuccess(false);
	}
	

	@RequestMapping(value = "/unblockStudent", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> unblockStudent(@RequestBody IdForm form) {
		if (StringUtils.equalsIgnoreCase(loadCurrentAccount().getType(), "admin")) {
			return returnSuccess(bizStudent.unblock(form.getId()));
		}
		return returnSuccess(false);
	}
	
	
	@RequestMapping(value = "/loadAllTeachers", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<DefaultPagingBody<AccountEntity>>> loadAllTeachers(@RequestBody PagableForm page) {
			return returnSuccess(bizTeacher.loadTeachers(page));
	}
	
	@RequestMapping(value = "/blockTeacher", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> blockTeacher(@RequestBody IdForm form) {
		if (StringUtils.equalsIgnoreCase(loadCurrentAccount().getType(), "admin")) {
			return returnSuccess(bizTeacher.block(form.getId()));
		}
		return returnSuccess(false);
	}
	

	@RequestMapping(value = "/unblockTeacher", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> unblockTeacher(@RequestBody IdForm form) {
		if (StringUtils.equalsIgnoreCase(loadCurrentAccount().getType(), "admin")) {
			return returnSuccess(bizTeacher.unblock(form.getId()));
		}
		return returnSuccess(false);
	}
	

	@RequestMapping(value = "/blockOrg", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> blockOrg(@RequestBody IdForm form) {
		if (StringUtils.equalsIgnoreCase(loadCurrentAccount().getType(), "admin")) {
			return returnSuccess(orgBusiness.block(form.getId()));
		}
		return returnSuccess(false);
	}
	

	@RequestMapping(value = "/unblockOrg", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> unblockOrg(@RequestBody IdForm form) {
		if (StringUtils.equalsIgnoreCase(loadCurrentAccount().getType(), "admin")) {
			return returnSuccess(orgBusiness.unblock(form.getId()));
		}
		return returnSuccess(false);
	}
	
	
	@RequestMapping(value = "/loadTeacherById", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<AccountEntity>> loadTeacher(@RequestBody IdForm form) {
		if (StringUtils.equalsIgnoreCase(loadCurrentAccount().getType(), "admin")) {
			return returnSuccess(bizTeacher.loadTeacher(form.getId()));
		}
		return returnSuccess(null);
	}
	
	@RequestMapping(value = "/loadAccountCount", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<IntegerForm>> loadAccountCount(@RequestBody IdForm form){
		IntegerForm res = new IntegerForm();
		
		res.setTeacherNumb(bizTeacher.countByOrgId(form.getId()));
		res.setStudentNumb(bizStudent.countByOrgId(form.getId()));
		res.setStoreNumb(bizStore.countByOrgId(form.getId()));
		return returnSuccess(res);
	}

	@RequestMapping(value = "/loadOrgDetail", consumes = "application/json", method = { RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<AccountEntity>> loadOrgDetail(@RequestBody IdForm form){
		AccountEntity res = orgBusiness.loadOrgDetail(form.getId());
		if(res!=null) {
			res.maskPassword();
		}
		return returnSuccess(res);
	}
	
}
